/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.grammar.util;

import java.util.List;

public final class ExpressionUtil
{
    /**
     * List the toString out put of the objects in the List comma separated. If
     * the List is null or empty an empty string is returned.
     * <p/>
     * The same as getStringList(list, true, false)
     *
     * @param list list of objects with toString methods
     * @return comma separated list of the elements in the list
     * @see #getStringList(java.util.List, boolean, boolean)
     */
    public static String getStringList(List list)
    {
        return getStringList(list, true, false);
    }

    /**
     * List the toString out put of the objects in the List that can be comma separated. If
     * the List is null or empty an empty string is returned.
     *
     * @param list        list of objects with toString methods
     * @param useComma    true if the list has to be comma separated
     * @param useBrackets true if the list has to be enclosed in brackets
     * @return comma separated list of the elements in the list
     */
    public static String getStringList(List list, boolean useComma, boolean useBrackets)
    {
        String ans = "";
        String comma = ",";
        if (!useComma)
        {
            comma = "";
        }
        if (list != null)
        {
            if (useBrackets)
            {
                ans += "(";
            }

            for (int i = 0; i < list.size(); i++)
            {
                ans += "" + list.get(i) + ((i < list.size() - 1) ? comma + " " : "");
            }

            if (useBrackets)
            {
                ans += ")";
            }
        }

        return ans;
    }

}
